package com.wf.demo.info.web;

import com.wf.demo.info.pojo.Model;
import com.wf.demo.info.service.ModelService;
import com.wf.demo.info.util.Page4Navigator;
import com.wf.demo.info.util.Result;
import com.wf.demo.sys.annotation.SysLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ModelController {
    @Autowired
    ModelService modelService;
    @GetMapping("/models")
    @RequiresPermissions("info:model")
    public List<Model> list(){
        return  modelService.list();
    }

    @GetMapping("/models/{id}")
    @RequiresPermissions("info:model")
    public Object get(@PathVariable("id")int id){
        Model bean= modelService.get(id);
        return bean;
    }
    @SysLog("保存机型")
    @PostMapping("/models")
    @RequiresPermissions("info:model")
    public Object add(@RequestBody Model bean) throws Exception{
        String name =bean.getModelName();
        String number=bean.getModelNumber();
        int  exist=modelService.isExist(name,number);
        if(exist == 1){
            String message ="此机型名已存在，无需重复添加";
            return Result.addFail(message);
        }
        if(exist == 2){
            String message ="此机型编号已存在，无需重复添加";
            return Result.addFail(message);
        }
        //无重复
        modelService.add(bean);
        //初始化
        //modelService.modelInit();
        return Result.addSuccess(bean);
    }
    @SysLog("删除机型")
    @DeleteMapping("/models/{id}")
    @RequiresPermissions("info:model")
    public String delete(@PathVariable("id")int id){
        modelService.delete(id);
        return null;
    }

    @SysLog("修改机型")
    @PutMapping("/models")
    @RequiresPermissions("info:model")
    public Object update(@RequestBody Model bean) throws Exception{

        int  exist=modelService.isExist(bean);
        if(exist == 1){
            String message ="此机型名已存在，更新失败";
            return Result.addFail(message);
        }
        if(exist == 2){
            String message ="此机型编号已存在，更新失败";
            return Result.addFail(message);
        }
        //无重复
        modelService.update(bean);

        return Result.addSuccess(bean);
    }


}
